package com.controller.base;

import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.utils.DateUtils;
import com.common.utils.EntityConversionUtil;
import com.common.utils.PythonUtil;
import com.common.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import com.model.base.PageResult;
import com.model.base.Result;
import com.model.domain.base.GetBaseCarInfoDO;
import com.model.domain.base.GetBaseServiceStationInfoDO;
import com.model.domain.order.GetOrderRepairRecordOverviewDO;
import com.model.dto.base.car.GetCarAllDTO;
import com.model.dto.base.car.GetCarInfoDTO;
import com.model.dto.base.car.GetCarListDTO;
import com.model.dto.base.car.GetCarRouteDTO;
import com.model.dto.base.car.GetCarSiteRouteDTO;
import com.model.dto.base.car.GetCarTotalDTO;
import com.model.pojo.base.BaseCarInfo;
import com.model.pojo.base.BaseServiceStationInfo;
import com.model.vo.base.car.getCarList.GetCarAllVO;
import com.model.vo.base.car.getCarList.GetCarAllocationVO;
import com.model.vo.base.car.getCarList.GetCarDistributionVO;
import com.model.vo.base.car.getCarList.GetCarInfoVO;
import com.model.vo.base.car.getCarList.GetCarListVO;
import com.model.vo.base.car.getCarList.GetCarRouteListVO;
import com.model.vo.base.car.getCarList.GetCarRouteVO;
import com.model.vo.base.car.getCarList.GetCarSiteRouteListVO;
import com.model.vo.base.car.getCarList.GetCarTotalVO;
import com.model.vo.base.car.getCarList.GetServiceCarNoListVO;
import com.service.base.BaseCarInfoService;
import com.service.base.BaseServiceStationInfoService;
import com.service.order.OrderRepairRecordInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @Description
 * @Author tm
 * @Date 2023-09-09 15:43
 **/
@Api(tags = {"服务车管理"})
@Slf4j
@RestController
@RequestMapping("/v1/base/car")
@RequiredArgsConstructor
public class CarInfoController {
    private final BaseCarInfoService baseCarInfoService;
    private final OrderRepairRecordInfoService repairRecordInfoService;
    private final BaseServiceStationInfoService baseServiceStationInfoService;
    @Log(title = "服务车管理-服务车列表", businessType = BusinessType.SELECT)
    @ApiOperation(value = "服务车列表")
    @PostMapping("/getCarList")
    public Result<PageResult<List<GetCarListVO>>> getCarList(@RequestBody @Valid GetCarListDTO dto){
        List<GetCarListVO> voList = new ArrayList<>();
        GetBaseCarInfoDO getBaseCarInfoDO = EntityConversionUtil.copy(dto,GetBaseCarInfoDO.class);
        if(StringUtils.isNotEmpty(getBaseCarInfoDO.getProvince())){
            String province = getBaseCarInfoDO.getProvince().replace("省", "");
            getBaseCarInfoDO.setProvince(province);
        }
        List<BaseCarInfo> list = baseCarInfoService.getPageList(getBaseCarInfoDO);
        voList = EntityConversionUtil.copyList(list,GetCarListVO.class);
        PageInfo<BaseCarInfo> pageInfo = new PageInfo<>(list);
        PageResult<List<GetCarListVO>> build = PageResult.<List<GetCarListVO>>builder().result(voList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }

    @Log(title = "服务车管理-服务车ALL数据", businessType = BusinessType.SELECT)
    @ApiOperation(value = "服务车ALL数据")
    @PostMapping("/getCarAll")
    public Result<List<GetCarAllVO>> getCarAll(@RequestBody @Valid GetCarAllDTO dto){
        List<GetCarAllVO> voList = new ArrayList<>();
        GetBaseCarInfoDO getBaseCarInfoDO = EntityConversionUtil.copy(dto,GetBaseCarInfoDO.class);
        List<BaseCarInfo> list = baseCarInfoService.getList(getBaseCarInfoDO);
        list=list.subList(0,list.size()>100?100:list.size());
        voList = EntityConversionUtil.copyList(list,GetCarAllVO.class);
        return Result.suc(voList);
    }
    @Log(title = "服务车管理-服务车总数", businessType = BusinessType.SELECT)
    @ApiOperation(value = "服务车总数")
    @PostMapping("/getCarTotal")
    public Result<GetCarTotalVO> getCarTotal(@RequestBody @Valid GetCarTotalDTO dto){
        int carTotal = baseCarInfoService.getCarTotal(dto);
        return Result.suc(new GetCarTotalVO(carTotal));
    }

    @Log(title = "服务车管理-服务车分布", businessType = BusinessType.SELECT)
    @ApiOperation(value = "服务车分布")
    @PostMapping("/getCarDistribution")
    public Result<GetCarDistributionVO> getCarDistribution(@RequestBody @Valid GetCarInfoDTO dto){
        BaseCarInfo baseCarInfo = baseCarInfoService.getBaseCarInfo(dto);
        GetCarInfoVO carInfoVO =EntityConversionUtil.copy(baseCarInfo,GetCarInfoVO.class);
        GetCarDistributionVO vo = new GetCarDistributionVO();
        vo.setCarInfoVO(carInfoVO);
        Date date =new Date();
        String time = DateUtils.parseDateToStr(date, DateUtils.YYYY_MM_DD);
        List<GetOrderRepairRecordOverviewDO> overviewDOList = repairRecordInfoService.selectByCarIdCount(carInfoVO.getCarId(), time);
        List<GetServiceCarNoListVO> serviceCarNoList =EntityConversionUtil.copyList(overviewDOList,GetServiceCarNoListVO.class);
        vo.setServiceCarNoList(serviceCarNoList);
        vo.setTime(date);
        return Result.suc(vo);
    }

    @Log(title = "服务车管理-服务车分配", businessType = BusinessType.SELECT)
    @ApiOperation(value = "服务车分配")
    @PostMapping("/getCarAllocation")
    public Result<List<GetCarAllocationVO>> getCarAllocation(@RequestBody @Valid GetCarTotalDTO dto){
        List<GetCarAllocationVO> voList = new ArrayList<>();
        String province = dto.getProvince();
        province = province.replaceAll("省","");
        /*String params = dto.getCarNum() +" "+ province;
        List<Map<String,String>> list = PythonUtil.callPathMap(params);
        if(list!=null && list.size()>0){
            for(Map<String,String> map:list){
                GetCarAllocationVO allocationVO=new GetCarAllocationVO();
                allocationVO.setName(map.get("name"));
                allocationVO.setTotal(map.get("number"));
                vo.add(allocationVO);
            }
        }*/
        GetBaseServiceStationInfoDO getBaseServiceStationInfoDO = new GetBaseServiceStationInfoDO();
        getBaseServiceStationInfoDO.setPage(1);
        getBaseServiceStationInfoDO.setPageSize(10);
        getBaseServiceStationInfoDO.setSortRow("stationId");
        getBaseServiceStationInfoDO.setProvince(province);
        List<BaseServiceStationInfo> stationList = baseServiceStationInfoService.getPageList(getBaseServiceStationInfoDO);
        if(stationList!=null && stationList.size()>0){
            stationList.forEach(item->{
                GetCarAllocationVO vo = new GetCarAllocationVO();
                vo.setName(item.getStationName());
                vo.setTotal("车辆总数："+item.getCarNum());
                vo.setLon(item.getLon());
                vo.setLat(item.getLat());
                voList.add(vo);
            });
        }
        return Result.suc(voList);
    }

    @Log(title = "服务车管理-服务车路线", businessType = BusinessType.SELECT)
    @ApiOperation(value = "服务车路线")
    @PostMapping("/getCarRouteList")
    public Result<List<GetCarRouteListVO>> getCarRouteList(@RequestBody @Valid GetCarRouteDTO dto){
        List<GetCarRouteListVO> voList = new ArrayList<>();
        GetBaseCarInfoDO getBaseCarInfoDO = new GetBaseCarInfoDO();
        getBaseCarInfoDO.setPage(1);
        getBaseCarInfoDO.setPageSize(5);
        List<BaseCarInfo> carList = baseCarInfoService.getPageList(getBaseCarInfoDO);
        if(carList!=null && carList.size()>0){
            for(int i=0;i<carList.size();i++){
                BaseCarInfo carInfo = carList.get(i);
                GetCarRouteListVO carRouteListVO=new GetCarRouteListVO();
                carRouteListVO.setCarNo(carInfo.getCarNo());
                carRouteListVO.setPlateNumber(carInfo.getPlateNumber());
                carRouteListVO.setAddress(carInfo.getAddress());
                carRouteListVO.setOwnerName(carInfo.getOwnerName());
                carRouteListVO.setCellphone(carInfo.getCellphone());
                List<GetCarRouteVO> carRouteList=getCarRouteList(i);
                carRouteListVO.setCarRouteList(carRouteList);
                voList.add(carRouteListVO);
            }
        }
        /*List<Map<String,String>> list = PythonUtil.callPathPlanning();
        if(list!=null && list.size()>0){
            int index = 0;
            for(int i=0;i<list.size();i++){
                Map<String,String> map = list.get(i);
                BaseCarInfo carInfo = carList.get(i);
                GetCarRouteListVO carRouteListVO=new GetCarRouteListVO();
                carRouteListVO.setCarNo(carInfo.getCarNo());
                carRouteListVO.setPlateNumber(carInfo.getPlateNumber());
                carRouteListVO.setAddress(carInfo.getAddress());
                carRouteListVO.setOwnerName(carInfo.getOwnerName());
                carRouteListVO.setCellphone(carInfo.getCellphone());
                List<GetCarRouteVO> carRouteList=new ArrayList<>();
                String[] ids = map.get("id").split(",");
                for(String id:ids){
                    GetCarRouteVO carRouteVO=new GetCarRouteVO();
                    BaseCarInfo cInfo = carList.get(index);
                    carRouteVO.setLat(cInfo.getLat());
                    carRouteVO.setLon(cInfo.getLon());
                    carRouteVO.setNum(id);
                    carRouteList.add(carRouteVO);
                    if(index<11)index++;
                    else if(index == 11)index = 0;
                }
                carRouteListVO.setCarRouteList(carRouteList);
                vo.add(carRouteListVO);
            }
        }*/
        return Result.suc(voList);
    }


    @Log(title = "服务车管理-服务车站点路线", businessType = BusinessType.SELECT)
    @ApiOperation(value = "服务车站点路线")
    @PostMapping("/getCarSiteRouteList")
    public Result<List<GetCarSiteRouteListVO>> getCarSiteRouteList(@RequestBody @Valid GetCarSiteRouteDTO dto){
        List<GetCarSiteRouteListVO> vo = new ArrayList<>();
        GetBaseCarInfoDO getBaseCarInfoDO = new GetBaseCarInfoDO();
        List<BaseCarInfo> carList = baseCarInfoService.getAllList(getBaseCarInfoDO);
        List<Map<String,String>> list = PythonUtil.callDD();
        if(list!=null && list.size()>0){
            int index = 0;
            for(int i=0;i<list.size();i++){
                Map<String,String> map = list.get(i);
                GetCarSiteRouteListVO carSiteRouteListVO=new GetCarSiteRouteListVO();
                carSiteRouteListVO.setSiteNo("S09817282");
                carSiteRouteListVO.setSiteName(map.get("name"));
                carSiteRouteListVO.setAddress("山东省德州市宁津县");
                List<GetCarRouteVO> carRouteList=new ArrayList<>();
                String[] ids = map.get("id").split(",");
                for(String id:ids){
                    GetCarRouteVO carRouteVO=new GetCarRouteVO();
                    BaseCarInfo cInfo = carList.get(index);
                    carRouteVO.setLat(cInfo.getLat());
                    carRouteVO.setLon(cInfo.getLon());
                    carRouteVO.setNum(id);
                    carRouteList.add(carRouteVO);
                    if(index<11)index++;
                    else if(index == 11)index = 0;
                }
                carSiteRouteListVO.setCarRouteList(carRouteList);
                vo.add(carSiteRouteListVO);
            }
        }

        return Result.suc(vo);
    }
    private List<GetCarRouteVO> getCarRouteList(int index){
        List<GetCarRouteVO> list = new ArrayList<>();
        if(index == 0 ){
            GetCarRouteVO vo = new GetCarRouteVO();
            vo.setNum("27");
            vo.setLon("112.868916667");
            vo.setLat("34.563231667");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("42");
            vo.setLon("113.07783453333333");
            vo.setLat("34.608679716666664");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("8");
            vo.setLon("113.21917216666667");
            vo.setLat("34.55603516666667");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("38");
            vo.setLon("113.41345776666667");
            vo.setLat("34.55649055");
            list.add(vo);
        }else if(index == 1 ){
            GetCarRouteVO vo = new GetCarRouteVO();
            vo.setNum("6");
            vo.setLon("112.730666667");
            vo.setLat("34.460673333");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("21");
            vo.setLon("112.73205308333333");
            vo.setLat("34.46227725");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("20");
            vo.setLon("112.74959765");
            vo.setLat("34.4710294");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("34");
            vo.setLon("112.8688799");
            vo.setLat("34.43466913333334");
            list.add(vo);
        }else if(index == 2 ){
            GetCarRouteVO vo = new GetCarRouteVO();
            vo.setNum("32");
            vo.setLon("113.57460655");
            vo.setLat("34.42729223333333");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("10");
            vo.setLon("113.55710061666667");
            vo.setLat("34.41027686666666");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("22");
            vo.setLon("113.54615731666667");
            vo.setLat("34.364568483333336");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("2");
            vo.setLon("113.56293063333334");
            vo.setLat("34.3424178");
            list.add(vo);
        }else if(index == 3 ){
            GetCarRouteVO vo = new GetCarRouteVO();
            vo.setNum("45");
            vo.setLon("113.11452061666667");
            vo.setLat("34.046236283333336");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("5");
            vo.setLon("113.1594873");
            vo.setLat("34.02659586666667");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("4");
            vo.setLon("113.2196219");
            vo.setLat("34.04920065");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("35");
            vo.setLon("113.16396448333333");
            vo.setLat("33.9958219");
            list.add(vo);
        }else if(index == 4 ){
            GetCarRouteVO vo = new GetCarRouteVO();
            vo.setNum("11");
            vo.setLon("113.360016667");
            vo.setLat("34.059136667");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("7");
            vo.setLon("113.34755728333333");
            vo.setLat("34.037691716666664");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("47");
            vo.setLon("113.37419588333333");
            vo.setLat("34.02599781666667");
            list.add(vo);
            vo = new GetCarRouteVO();
            vo.setNum("43");
            vo.setLon("113.39287093333333");
            vo.setLat("34.03222803333333");
            list.add(vo);
        }
        return list;
    }
}
